﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace Hosting.Class
{
    public class ArticleGroups
    {

        HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();

        public ArticleGroups() { }

        #region [Get all Article Group]
        /// <summary>
        /// Get all Article Group
        /// </summary>
        /// <param name="getActive">
        /// <para>0     : false</para>
        /// <para>1     : true</para>
        /// <para>-1    : get all</para>
        /// </param>
        /// <returns>
        /// <para>null                              : There is an error</para>
        /// <para>ToList().Count greater than 0     : Success</para>
        /// <para>ToList().Count less-equal than 0  : There are not any values</para>
        /// </returns>
        public List<PC_ArticleGroups_GetAllResult> getAll(string getActive)
        {
            try
            {
                var groups = db.PC_ArticleGroups_GetAll(Int32.Parse(getActive)).ToList();
                return groups;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region [Get one Article Group]
        /// <summary>
        /// Get one Article Group
        /// </summary>
        /// <param name="id">ID of Article Group</param>
        /// <returns>
        /// <para>null                              : There is an error</para>
        /// <para>ToList().Count greater than 0     : Success</para>
        /// <para>ToList().Count less-equal than 0  : There are not any values</para>
        /// </returns>
        public List<PC_ArticleGroups_GetOneResult> getOne(string id)
        {
            try
            {
                var groups = db.PC_ArticleGroups_GetOne(Int32.Parse(id)).ToList();
                return groups;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region [Insert a new article group into database]
        /// <summary>
        /// Insert a new article group into database
        /// </summary>
        /// <param name="name">Name of Article group.</param>
        /// <param name="description">A short description for group.</param>
        /// <param name="active">Visible/Hidden this group on Interface.</param>
        /// <returns>0: successfully</returns>
        /// <returns>-99: error, check log file.</returns>
        public int Add(string name, string description, bool active)
        {
            try
            {
                int isInsert = db.PC_ArticleGroups_Ins(name, description, active);
                
                return isInsert;
            }
            catch (Exception ex)
            {
                
                return -99;
            }
        }
        #endregion

        #region [Check Insert is valid or not]
        /// <summary>
        /// Check Insert is valid or not
        /// </summary>
        /// <param name="name">Name of Article Group</param>
        /// <param name="description">A short description about Article Group</param>
        /// <returns>
        /// <para>1    : Success</para>
        /// <para>10   : Article Group Name is null or empty</para>
        /// <para>11   : Article Group Name is greater than 50</para>
        /// <para>20   : Article Group Description is greater than 2000</para>
        /// </returns>
        public int AddValid(string name, string description)
        {
            if (String.IsNullOrEmpty(name))
            {
                return 10;
            }
            else if(name.Length > 50)
            {
                return 11;
            }
            else if(description.Length > 2000)
            {
                return 20;
            }

            return 1;
        }
        #endregion

        #region [Update an existed Article Group]
        /// <summary>
        /// Update an existed Article Group
        /// </summary>
        /// <param name="groupId">ID of Article Group</param>
        /// <param name="name">Name of Article Group</param>
        /// <param name="description">A short description about Article Group</param>
        /// <param name="active">Visible/Hidden Article in interface</param>
        /// <returns>
        /// <para>0    : success</para>
        /// <para>-99  : error</para>
        /// </returns>
        public int Edit(string groupId, string name, string description, bool active)
        {
            try
            {
                int isInsert = db.PC_ArticleGroups_Upd(Int32.Parse(groupId), name, description, active);
                return isInsert;
            }
            catch (Exception ex)
            {
                return -99;
            }
        }
        #endregion

        #region [Check Edit is valid or not]
        /// <summary>
        /// Check Edit is valid or not
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <returns>
        /// <para>1    : Success</para>
        /// <para>2    : GroupId is null or empty</para>
        /// <para>3    : GroupId is not valid</para>
        /// <para>10   : Article Group Name is null or empty</para>
        /// <para>11   : Article Group Name is greater than 50</para>
        /// <para>20   : Article Group Description is greater than 2000</para>
        /// </returns>
        public int EditValid(string groupId, string name, string description)
        {
            if(String.IsNullOrEmpty(groupId))
            {
                return 2;
            }

            Regex rGroupId = new Regex("^[0-9]{0,}$");
            Match mGroupId = rGroupId.Match(groupId);
            if(!mGroupId.Success)
            {
                return 3;
            }
            else if (String.IsNullOrEmpty(name))
            {
                return 10;
            }
            else if (name.Length > 50)
            {
                return 11;
            }
            else if (description.Length > 2000)
            {
                return 20;
            }

            return 1;
        }
        #endregion

        #region [Delete an existed Article Group]
        /// <summary>
        /// Delete an existed Article Group
        /// </summary>
        /// <param name="id">ID of Article Group</param>
        /// <returns>
        /// <para>0     : successfully</para>
        /// <para>-99   : error</para>
        /// </returns>
        public int delete(string id)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isDelete = db.PC_ArticleGroups_Del(Int32.Parse(id));
                return isDelete;
            }
            catch(Exception ex)
            {
                return -99;
            }
        }
        #endregion

    }
}

